import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
//下载yarn add vuex-persistedstate@3.2.1
//import createPersistedState from 'vuex-persistedstate'
//Store：{plugins:[createPersistedState()]}
//key 指定持久化的名字，默认vuex,
//storage:选用一种持久化方案，默认本地存储
//reduce：用于指定哪些属性需要持久化，不指定reduce,默认vuex里所有数据都会被持久化
//reduce是一个函数，这个函数必须返回一个对象，这个对象会持久化，参数为state
export default new Vuex.Store({
  plugins: [
    createPersistedState({
      key: 'HEIMA_TOUTIAO',
      reduce({ tokenObj, myChannels, histories }) {
        return { tokenObj, myChannels, histories }
      }
    })
  ],
  state: {
    tokenObj: {},
    myChannels: [],
    histories: []
  },
  getters: {
    isLogin(state) {
      return !!state.tokenObj.token
    }
  },
  mutations: {
    SET_TOKEN(state, tokenObj) {
      state.tokenObj = tokenObj
    },
    MY_CHANNEL(state, myChannels) {
      state.myChannels = myChannels
    },
    SET_HISTORY(state, histories) {
      state.histories = histories
    }
  },
  actions: {},
  modules: {}
})
